package com.atguigu.yygh.hosp.api;


import com.atguigu.yygh.common.exception.HospitalException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.order.service.DepartmentService;
import com.atguigu.yygh.order.service.HospitalService1;
import com.atguigu.yygh.order.service.HospitalSetService;
import com.atguigu.yygh.order.service.ScheduleService;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService1 hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;


    @ApiOperation("上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
       Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
       //参数校验
        String hoscode=(String)map.get("hoscode");
        if(StringUtils.isEmpty(hoscode)){
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String)map.get("logoData");
        if(!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            map.put("logoData", logoData);
        }
        //签名校验
        if(!HttpRequestHelper.isSignEquals(map, hospitalSetService.getSignKey(hoscode))) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

       hospitalService.save(map);
        return Result.ok();
    }
    @ApiOperation("显示医院")
    @PostMapping("hospital/show")
     public Result showHospital(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        String hoscode=(String)map.get("hoscode");
        //签名校验
        if(!HttpRequestHelper.isSignEquals(map, hospitalSetService.getSignKey(hoscode))) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
       return Result.ok(hospitalService.findByHosCode(map));

     }
    @ApiOperation("上传科室,通过医院hoscode")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        String hoscode=(String)map.get("hoscode");
        //签名校验
        if(!HttpRequestHelper.isSignEquals(map, hospitalSetService.getSignKey(hoscode))) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.saveDepartmentByHoscode(map);
        //要有分页和条件
        return Result.ok();

    }
    @ApiOperation("显示科室科室,通过医院hoscode")
    @PostMapping("department/list")
    public Result showDepartment(HttpServletRequest request,Integer page,Integer limit){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode=(String)map.get("hoscode");
        if( StringUtils.isEmpty(hoscode)){
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        if(!HttpRequestHelper.isSignEquals(map, hospitalSetService.getSignKey(hoscode))) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }


        //分页非必填参数的默认值
        page= page==null?
                1:page;
        limit= limit==null?
                1:limit;


        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> departments = departmentService.selectPage(page, limit, departmentQueryVo);
        //要有分页和条件
        return Result.ok(departments);

    }
    @ApiOperation("删除科室科室,通过医院hoscode,和科室id")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request,Integer page,Integer limit){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode=(String)map.get("hoscode");
        String depcode=(String)map.get("depcode");
        if( StringUtils.isEmpty(hoscode) || StringUtils.isEmpty(depcode)){
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        if(!HttpRequestHelper.isSignEquals(map, hospitalSetService.getSignKey(hoscode))) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);

        departmentService.removeByHoscodeAndDepcode(departmentQueryVo);

        //要有分页和条件
        return Result.ok();

    }

    @ApiOperation("上传排班,通过医院hoscode,和科室id")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        String hoscode=(String)map.get("hoscode");
        if(StringUtils.isEmpty(hoscode)){
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }

        //签名校验
        if(!HttpRequestHelper.isSignEquals(map, hospitalSetService.getSignKey(hoscode))) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.saveSchedule(map);
        return Result.ok();

    }


    @ApiOperation("查询排班,通过医院hoscode,和科室id")
    @PostMapping("schedule/list")
    public Result showSchedule(HttpServletRequest request,Integer page,Integer limit){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode=(String)map.get("hoscode");
        if( StringUtils.isEmpty(hoscode)){
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        if(!HttpRequestHelper.isSignEquals(map, hospitalSetService.getSignKey(hoscode))) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }


        //分页非必填参数的默认值
        page= page==null?
                1:page;
        limit= limit==null?
                1:limit;


        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);

        Page<Schedule> schedules = scheduleService.selectPage(page, limit, scheduleQueryVo);
        //要有分页和条件
        return Result.ok(schedules);

    }
    @ApiOperation("删除科室科室,通过医院hoscode,和科室id")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());

        String hoscode=(String)map.get("hoscode");
        String hosScheduleId=(String)map.get("hosScheduleId");
        if( StringUtils.isEmpty(hoscode) || StringUtils.isEmpty(hosScheduleId)){
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        if(!HttpRequestHelper.isSignEquals(map, hospitalSetService.getSignKey(hoscode))) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(hosScheduleId);

        scheduleService.removeByHoscodeAndHosScheduleId(scheduleQueryVo);

        //要有分页和条件
        return Result.ok();

    }






}